package example

import (
	"gitee.com/Luna-CY/Golang-Project-Template/entry/http/request"
	"gitee.com/Luna-CY/Golang-Project-Template/entry/http/response"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context/contextutil"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/i18n"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/dao"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/service"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/runtime"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"github.com/gin-gonic/gin"
)

type CreateRequest struct {
	CaptchaKey  string `json:"captcha_key" validate:"required" binding:"required"`                                         // 验证码的key
	CaptchaDots string `json:"captcha_dots" validate:"required" binding:"required"`                                        // 验证码的坐标
	Name        string `json:"name" validate:"required" maximum:"255" binding:"required,max=255"`                          // 字符串字段
	Value       uint64 `json:"value" validate:"required" minimum:"1" maximum:"999999" binding:"required,min=1,max=999999"` // 数值字段
	Enable      bool   `json:"enable" validate:"optional" binding:"omitempty"`                                             // 布尔字段
	Type        int    `json:"type" validate:"required" enums:"1,2,3" binding:"required,oneof=1 2 3"`                      // 枚举字段, enums: 1 - A, 2 - B, 3 - C
	ImageId     string `json:"image_id" validate:"optional" binding:"omitempty"`                                           // 文件字段
}

// Create Create
// @tags category/example
// @summary create
// @param param body CreateRequest true "request body"
// @success 200 {object} response.Response{} "successful. click to expand response structure"
// @router /example/create [post]
func (cls *Example) Create(c *gin.Context) (response.Code, any, errors.I18nError) {
	var body = CreateRequest{}
	if err := request.ShouldBindJSON(c, &body); nil != err {
		return response.InvalidParams, nil, errors.NewI18n(i18n.CommonIdInvalidRequest, err.Relation(errors.ErrorInvalidRequest("SHGWHE_LE.E_LE.C_TE.292902", "参数错误")))
	}

	var ctx = contextutil.NewContextWithGin(c)

	if !runtime.IsDevelopment() {
		// 验证验证码
		if err := cls.captcha.CheckClickCaptcha(ctx, body.CaptchaKey, body.CaptchaDots, true); nil != err {
			return response.CaptchaFailed, nil, errors.NewI18n(i18n.CommonIdInvalidRequest, err.Relation(errors.ErrorInvalidRequest("SHGWHE_LE.E_LE.C_TE.371908", "验证码错误")))
		}
	}

	var fields = []service.BusinessExampleField{
		service.BusinessExampleFieldWithEnable(body.Enable),
		service.BusinessExampleFieldWithType(model.BusinessExampleEnumFieldType(body.Type)),
	}

	if "" != body.ImageId {
		// 获取文件
		image, err := cls.storage.GetObjectByCondition(ctx, dao.SystemObjectOptionWithId(body.ImageId), dao.SystemObjectOptionWithStatus(model.SystemObjectStatusUploaded))
		if nil != err {
			if err.IsType(errors.ErrorTypeRecordNotFound) {
				return response.InvalidParams, nil, errors.NewI18n(i18n.CommonIdInvalidRequest, err.Relation(errors.ErrorInvalidRequest("SHGWHE_LE.E_LE.C_TE.461518", "文件不存在")))
			}

			return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGWHE_LE.E_LE.C_TE.491530", "获取文件失败")))
		}

		if err := cls.storage.PersistenceObject(ctx, image); nil != err {
			return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGWHE_LE.E_LE.C_TE.631108", "保存文件失败")))
		}

		fields = append(fields, service.BusinessExampleFieldWithImageId(image))
	}

	// 创建数据
	if _, err := cls.example.CreateExample(ctx, body.Name, body.Value, fields...); nil != err {
		return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGWHE_LE.E_LE.C_TE.342933", "创建数据失败")))
	}

	return response.Ok, nil, nil
}
